package pqstorer

//Implements the following interface:
//type HumanStorer interface {
//	SaveNew(*Human) error //Make sure to update human with Id!
//	Update(*Human) error
//	SetUserId(int64, int64) error
//	Delete(int64) error
//	GetByUserId(int64) (*Human, error)
//	GetAll() ([]Human, error)
//	GetHumansToBlur() ([]Human, error)
//	GetUnblurred() ([]Human, error)
//}

import (
	"code.google.com/p/jesus-1496/models"
	"code.google.com/p/jesus-1496/serverlog"
	"code.google.com/p/jesus-1496/utils/db"
	"fmt"
)

var logger = serverlog.NewServerLog("stores/pqstorer")

type HumanStore struct{}

func (p *HumanStore) SaveNew(h *models.Human) error {

	err := db.UnmarshallQuery(&(h.Id), `select create_human(
        $1::varchar(128),
        $2::boolean,
        $3::varchar(128),
        $4::varchar(128),
        $5::varchar(128),
        $6::latitude_t,
        $7::longitude_t,
        $8::integer,
        $9::integer,
        $10::varchar(128),
        $11::integer);`,
		h.Name,
		h.IsStudent,
		h.TwitterId,
		h.LinkedinId,
		h.FacebookId,
		h.GeoCoord.Latitude,
		h.GeoCoord.Longitude,
		h.PhotoCoord.X,
		h.PhotoCoord.Y,
		h.Subject,
		h.ClassId)
	if err != nil {
		return fmt.Errorf("Errorpoo: %v", err)
	}

	logger.Printf("Human %+v successfully saved to database.", h)
	return nil
}

func (p *HumanStore) Update(h *models.Human) error {
	_, err := db.Exec(`select update_human(
        $1::integer,
        $2::varchar(128),
        $3::boolean,
        $4::varchar(128),
        $5::varchar(128),
        $6::varchar(128),
        $7::latitude_t,
        $8::longitude_t,
        $9::integer,
        $10::integer,
        $11::varchar(128),
        $12::integer,
        $13::integer);`,
		h.Id,
		h.Name,
		h.IsStudent,
		h.TwitterId,
		h.LinkedinId,
		h.FacebookId,
		h.GeoCoord.Latitude,
		h.GeoCoord.Longitude,
		h.PhotoCoord.X,
		h.PhotoCoord.Y,
		h.Subject,
		h.UserId,
		h.ClassId)
	if err != nil {
		return err
	}
	return nil
}

func (p *HumanStore) SetUserId(id, userId int64) error {
	_, err := db.Exec(`select set_human_user_id($1::integer, $2::integer);`, id, userId)
	if err != nil {
		return err
	}
	return nil
}

func (p *HumanStore) Delete(id int64) error {
	_, err := db.Exec(`select delete_human($1::integer);`, id)
	if err != nil {
		return err
	}
	return nil
}

func (p *HumanStore) GetByUserId(userId int64) (*models.Human, error) {
	var ret models.Human
	err := db.UnmarshallQuery(&ret, `select get_human_by_user_id($1::integer);`, userId)
	if err != nil {
		return nil, err
	}
	return &ret, nil

}

func (p *HumanStore) GetAll() ([]models.Human, error) {
	var ret []models.Human
	err := db.UnmarshallQuery(&ret, `select get_all_humans();`)
	if err != nil {
		return nil, err
	}
	return ret, nil
}

func (p *HumanStore) GetHumansToBlur() ([]models.Human, error) {
	var ret []models.Human
	err := db.UnmarshallQuery(&ret, `select get_humans_to_blur();`)
	if err != nil {
		return nil, err
	}
	return ret, nil
}

func (p *HumanStore) GetUnblurred() ([]models.Human, error) {
	var ret []models.Human
	err := db.UnmarshallQuery(&ret, `select get_unblurred_humans();`)
	if err != nil {
		return nil, err
	}
	return ret, nil
}

func init() {
	models.SetHumanStore(&HumanStore{})
}
